﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WC.DL.DALC.IF;
using WC.BL.BE;
using SAP.Middleware.Connector;

namespace WC.DL.DALC.SAP
{
    public class HelperDALC : IHelperDALC
    {
        #region Singleton
        public static HelperDALC Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (typeof(HelperDALC))
                    {
                        if (_instance == null)
                        {
                            _instance = new HelperDALC();
                        }
                    }
                }
                return _instance;
            }
        }

        private HelperDALC() { }
        private static volatile HelperDALC _instance = null;
        #endregion


        #region IHelperDALC Members

        public List<GenericBE> getDptos()
        {
            IRfcFunction function = null; RfcDestination dest;
            GenericBE obj = null; List<GenericBE> lst = new List<GenericBE>();

            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_007");
                function.Invoke(dest);
                IRfcTable T_T005U = function.GetTable("T_T005U");
                if (T_T005U != null && T_T005U.Count > 0)
                {
                    foreach (IRfcStructure S_T005U in T_T005U)
                    {
                        obj = new GenericBE();
                        obj.Codigo = S_T005U["BLAND"].GetString();
                        obj.Descripcion = S_T005U["BEZEI"].GetString();

                        lst.Add(obj);
                    }
                }
            }
            catch
            {
                lst = new List<GenericBE>();
            }
            return lst;
        }

        public List<GenericBE> getProv(string pDpto)
        {
            IRfcFunction function = null; RfcDestination dest;
            GenericBE obj = null; List<GenericBE> lst = new List<GenericBE>();

            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_008");
                function.SetValue("I_BLAND", pDpto);
                function.Invoke(dest);
                IRfcTable T_PROV = function.GetTable("T_PROV");
                if (T_PROV != null && T_PROV.Count > 0)
                {
                    foreach (IRfcStructure S_PROV in T_PROV)
                    {
                        obj = new GenericBE();
                        obj.Codigo = S_PROV["PROV"].GetString();
                        obj.Descripcion = S_PROV["CITY1"].GetString();

                        lst.Add(obj);
                    }
                }
            }
            catch
            {
                lst = new List<GenericBE>();
            }
            return lst;
        }

        public List<GenericBE> getDist(string pDpto, string pProv)
        {
            IRfcFunction function = null; RfcDestination dest;
            GenericBE obj = null; List<GenericBE> lst = new List<GenericBE>();

            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_009");
                function.SetValue("I_BLAND", pDpto);
                function.SetValue("I_PROV", pProv);
                function.Invoke(dest);
                IRfcTable T_DIST = function.GetTable("T_DIST");
                if (T_DIST != null && T_DIST.Count > 0)
                {
                    foreach (IRfcStructure S_DIST in T_DIST)
                    {
                        obj = new GenericBE();
                        obj.Codigo = S_DIST["DIST"].GetString();
                        obj.Descripcion = S_DIST["CITY2"].GetString();

                        lst.Add(obj);
                    }
                }
            }
            catch
            {
                lst = new List<GenericBE>();
            }
            return lst;
        }

        public List<GenericBE> getTiendas(string pCod, string pDesc, string pRazonSocial)
        {
            IRfcFunction function = null; RfcDestination dest;
            GenericBE obj = null; List<GenericBE> lst = new List<GenericBE>();

            try
            {
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_SD_006");
                if (!string.IsNullOrEmpty(pCod))
                { function.SetValue("I_WERKS", pCod); }
                if (!string.IsNullOrEmpty(pRazonSocial))
                { function.SetValue("I_NAME1", pRazonSocial); }
                if (!string.IsNullOrEmpty(pDesc))
                { function.SetValue("I_NAME2", pDesc); }
                function.Invoke(dest);
                IRfcTable T_CENTROS = function.GetTable("T_CENTROS");
                if (T_CENTROS != null && T_CENTROS.Count > 0)
                {
                    obj = new GenericBE(); obj.Codigo = string.Empty; obj.Descripcion = string.Empty;
                    lst.Add(obj);
                    foreach (IRfcStructure S_CENTROS in T_CENTROS)
                    {
                        obj = new GenericBE();
                        obj.Codigo = S_CENTROS["WERKS"].GetString();
                        obj.Descripcion = S_CENTROS["NAME2"].GetString();

                        lst.Add(obj);
                    }
                }
            }
            catch
            {
                lst = new List<GenericBE>();
            }
            return lst;
        }

        #endregion
    }
}
